package com.semeureka.fault.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.semeureka.fault.bean.Device;
import com.semeureka.fault.bean.Device.Phase;
import com.semeureka.fault.bean.Group;
import com.semeureka.fault.dao.DeviceDao;
import com.semeureka.fault.service.DeviceService;
import com.semeureka.fault.util.Message;

@Component
@Transactional
public class DeviceServiceImpl implements DeviceService {
	@Autowired
	private DeviceDao deviceDao;

	@Override
	public void update(Message message) {
		if (message.getGroup() != null && message.getPhase() != null && message.getCreateTime() != null) {
			deviceDao.updateTime(message.getGroup(), message.getPhase(), message.getCreateTime());
		}
	}

	@Override
	@Cacheable(value = "devices", key = "T(com.semeureka.frame.util.HexUtils).toHex(#code)")
	public Device findByCode(byte[] code) {
		return deviceDao.findByCode(code);
	}

	@Override
	@Cacheable(value = "devices")
	public Device findByGroupPhase(Group group, Phase phase) {
		return deviceDao.findByGroupPhase(group, phase);
	}
}
